#pragma once

#include <assert.h>
using std::cout;
using std::endl;
//红黑树有两种颜色作为区分
enum Color
{
	RED,
	BLACK
};

template<class K,class V>
struct RBTNode 
{
	RBTNode<K, V>* _left;
	RBTNode<K, V>* _right;
	RBTNode<K, V>* _parent;

	std::pair<K, V> _kv;
	Color _col;
	RBTNode(const std::pair<K,V>& kv)
		:_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_kv(kv)
	{}
};

template<class K,class V>
class RBTree 
{
public:
	typedef RBTNode<K, V> Node;
	bool Insert(const std::pair<K, V>& kv)
	{
		//找到插入的位置
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				//相等 证明找到了 那就插入失败
				return false;
			}
		}
		//此时cur为空 
		cur = new Node(kv);
		cur->_col = RED;
		cur->_parent = parent;
		if (parent->_kv.first > kv.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		//插入部分完成 接下来要根据指定的规则进行红黑树的调整
	

		//只有当parent为红的时候 才需要调整 不然直接插入一个红节点就可以了
		while (parent && parent->_col == RED)
		{
			Node* grandparent = parent->_parent;
			assert(grandparent);
			assert(grandparent->_col == BLACK);

			if (parent == grandparent->_left)
			{
				Node* uncle = grandparent->_right;
				//情况一 uncle存在且为红  处理方案：p和u变黑 g变红  将g赋给cur g的p赋给parent 继续向上调整
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandparent->_col = RED;
					cur = grandparent;
					parent = cur->_parent;
				}//uncle存在且为红的情况 结束
				else// unclde不存在(cur为新增) 或者 存在且为黑(那这次变色一定是连锁反应导致的)
				{
					//情况二 unclde不存在 或者 存在为黑 且插入在了较高左树的左子树 处理方案：g右旋 p变黑 g变红
					if (cur == parent->_left)
					{
						//		g
						//	 p		u
						// c
						RotateR(grandparent);
						parent->_col = BLACK;
						grandparent->_col = RED;
						break;
					}//情况二结束
					else//情况三 unclde不存在 或者 存在为黑 且插入在了较高左树的右子树
					{
						//		g
						//  p		u
						//     c
						RotateL(parent);
						RotateR(grandparent);
						cur->_col = BLACK;
						grandparent->_col = RED;
						break;
					}//情况三结束
				}//uncle 不存在或者存在且为黑的情况  结束
			}//parent是grand的左子树情况处理完毕
			else//parent==grandparent->_right
			{
				Node* uncle = grandparent->_left;
				//情况一 uncle存在且为红
				if (uncle&& uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandparent->_col = RED;
					cur = grandparent;
					parent = cur->_parent;
				}
				else//uncld不存在 或者 存在为黑
				{
					if (cur == parent->_right)
					{
						RotateL(grandparent);
						parent->_col = BLACK;
						grandparent->_col = RED;
						break;
					}
					else
					{
						RotateR(parent);
						RotateL(grandparent);
						cur->_col = BLACK;
						grandparent->_col = RED;
						break;
					}
				}
				//parent是grand的右子树情况处理完毕
			}
		}
		_root->_col = BLACK;
		return true;
	}
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* ppNode = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (_root == parent)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = subR;
			}
			else
			{
				ppNode->_right = subR;
			}

			subR->_parent = ppNode;
		}

	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
		{
			subLR->_parent = parent;
		}

		Node* ppNode = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (_root == parent)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = subL;
			}
			else
			{
				ppNode->_right = subL;
			}

			subL->_parent = ppNode;
		}

	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	bool IsBalance()
	{
		if (_root == nullptr)
		{
			return true;
		}

		if (_root->_col == RED)
		{
			cout << "根节点不是黑色" << endl;
			return false;
		}

		// 黑色节点数量基准值
		int benchmark = 0;
		return PrevCheck(_root, 0, benchmark);
	}
private:
	bool PrevCheck(Node* root, int blackNum, int& benchmark)
	{
		if (root == nullptr)
		{
			//cout << blackNum << endl;
			//return;
			if (benchmark == 0)
			{
				benchmark = blackNum;
				return true;
			}

			if (blackNum != benchmark)
			{
				cout << "某条黑色节点的数量不相等" << endl;
				return false;
			}
			else
			{
				return true;
			}
		}

		if (root->_col == BLACK)
		{
			++blackNum;
		}

		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << "存在连续的红色节点" << endl;
			return false;
		}

		return PrevCheck(root->_left, blackNum, benchmark)
			&& PrevCheck(root->_right, blackNum, benchmark);
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}



private:
	Node* _root = nullptr;

};